package com.chuangke.metadata.controller;

import com.alibaba.druid.util.StringUtils;
import com.chuangke.common.db.page.CkPageQuery;
import com.chuangke.common.http.HttpResult;
import com.chuangke.common.idgen.IdManager;
import com.chuangke.metadata.entity.SysMoudle;
import com.chuangke.metadata.service.SysMoudleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author chuangke
 * @date "2024-01-09"
 */
@RestController
@RequestMapping("/sys/moudle")
public class SysMoudleController {

	@Autowired
	private SysMoudleService sysMoudleService;

	/**
	 * 查询字典类型列表
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping("/list")
	public HttpResult list(String name) {
		Map<String, Object> params = new HashMap<>();
		params.put("name", name);
		CkPageQuery query = new CkPageQuery(params);

		return HttpResult.ok(sysMoudleService.page(query));
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping("/page")
	public HttpResult page(@RequestBody Map<String, Object> params) {
		return HttpResult.ok(sysMoudleService.page(new CkPageQuery(params)));
	}

	/**
	 * 获取字典类型详细信息
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/{id}")
	public HttpResult getInfo(@PathVariable("id") String id) {
		return HttpResult.ok(sysMoudleService.findById(id));
	}

	/**
	 * 保存字典类型
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping
	public HttpResult save(@RequestBody SysMoudle action) {
		if(StringUtils.isEmpty(action.getId())){
			action.setId(IdManager.nextId());
		}
		sysMoudleService.save(action);
		return HttpResult.ok();
	}

	/**
	 * 删除字典类型
	 */
	@PreAuthorize("hasAuthority('login:action')")
	@DeleteMapping("/{ids}")
	public HttpResult remove(@PathVariable String[] ids) {
		sysMoudleService.deleteByIds(Arrays.asList(ids));
		return HttpResult.ok();
	}
}
